/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package javautil.locale;

import javautil.io.IOUtil;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author stewari1
 */
public class PropertiesUtil {

    public static void main(String[] args) {

        try {

            tidy(new File("C:/Documents and Settings/stewari1/Desktop/OrgInt.properties"),
                 new File("C:/Documents and Settings/stewari1/Desktop/OrgInt_out.properties"));

        } catch (IOException ex) {
            Logger.getLogger(PropertiesUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     *
     * @param inputFile
     * @param outputFile
     * @throws java.io.IOException
     *
     * @throws IOException description
     *
     * @throws java.io.IOException description
     */
    private static void tidy(File inputFile, File outputFile) throws IOException {

        String wholeString = IOUtil.readFileContentToString(inputFile);
        String[] lines     = wholeString.split("\n");
        String outputStr   = "";
        TreeMap map        = new TreeMap();

        for (String line : lines) {

            String[] parts = line.split("=");

            if (parts.length == 2) {
                map.put(parts[0], parts[1]);
            }

            if (parts.length == 1) {


                // keep the ones also that don`t have values keyed in
                map.put(parts[0], "");
            }
        }

        Iterator<String> itr = map.keySet().iterator();
        char ch              = 'A';

        while (itr.hasNext()) {

            String key = itr.next();

            if (ch != key.charAt(0)) {


                // give some blanks for beginning with new character
                ch        = key.charAt(0);
                outputStr += "\n";
            }

            String value = (String) map.get(key);

            outputStr += key + "=" + value + "\n";
        }

        IOUtil.writeToFile(outputFile, outputStr);
    }
}
